/**
 * str 全空字符串
 * str 字母数字字符串 aaaa55555554
 * str 字母数字空字符串 
 * str 字母、数字、空、运算符号，字符串 + aaa123456-1456ffff fff . * /
 * 使用for 循环再实现一次
 * https://leetcode.com/problems/string-to-integer-atoi/#/description
 * @param {*} str 
 */

var myAtoi = function (str) {
    return Math.max(Math.min(parseInt(str) || 0, 2147483647), -2147483648)
}

var myAtoi = function (str) {
    if (str.length === 0) { return 0 }
    let index = 0
    let type = 1
    let num = 0
    let sum = 0
    while (str.charAt(index) === ' ' && index < str.length) {
        index++
    }
    if (str.charAt(index) === '+' || str.charAt(index) === '-') {
        type = str.charAt(index) === '+' ? 1 : -1
        index++
    }

    while (index < str.length) {


        num = +str.charAt(index)
        if (num < 0 || num > 9) {
            break
        }
        sum = sum * 10 + num
        index++
    }
    return Math.max(Math.min(sum * type, 2147483647), -2147483648)
}